package zhh.tangbao.auxiliary.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhh.tangbao.auxiliary.common.ResponseStatus;
import zhh.tangbao.auxiliary.dao.SystemOpinionDao;
import zhh.tangbao.auxiliary.dao.SystemOpinionTypeDao;
import zhh.tangbao.auxiliary.dto.opinion.OpinionQueryDto;
import zhh.tangbao.auxiliary.dto.opinion.UpdateResponseDto;
import zhh.tangbao.auxiliary.entity.SystemOpinion;
import zhh.tangbao.auxiliary.entity.SystemOpinionType;
import zhh.tangbao.auxiliary.service.SystemOpinionService;
import zhh.tangbao.auxiliary.vo.opinion.OpinionInfoVo;
import zhh.tangbao.auxiliary.vo.opinion.OpinionPageVo;
import zhh.tangbao.auxiliary.vo.opinion.OpinionTypeSelectOptionVo;
import zhh.tangbao.system.entity.PagerEntity;
import zhh.tangbao.system.utils.UserHolderUtil;
import zhh.tangbao.user.dao.UserDao;
import zhh.tangbao.user.vo.UserVo;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 描述
 * </p>
 *
 * @author 粥灰灰
 * <p>2023/4/26</p>
 */

@Service
public class SystemOpinionServiceImpl implements SystemOpinionService {

    @Autowired
    private SystemOpinionDao dao;

    @Autowired
    private SystemOpinionTypeDao typeDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserHolderUtil userHolderUtil;

    @Override
    public PagerEntity<OpinionPageVo> page(PagerEntity<OpinionPageVo> pager, OpinionQueryDto dto) {
        List<OpinionPageVo> opinionPageVos = dao.listOpinionPageVos(pager, dto);
        Long count = dao.count(dto);
        pager.setData(opinionPageVos);
        pager.setTotalNum(count);
        if (CollUtil.isEmpty(opinionPageVos)) {
            return pager;
        }
        List<SystemOpinionType> systemOpinionTypes = typeDao.selectBatchIds(opinionPageVos.stream().map(OpinionPageVo::getTypeId).collect(Collectors.toList()));
        if (CollUtil.isEmpty(systemOpinionTypes)) {
            return pager;
        }
        Map<String, SystemOpinionType> map = systemOpinionTypes.stream().collect(Collectors.toMap(SystemOpinionType::getId, Function.identity()));
        for (int i = 0; i < opinionPageVos.size(); i++) {
            OpinionPageVo o = opinionPageVos.get(i);
            SystemOpinionType type = map.get(o.getTypeId());
            if (null != type) {
                o.setTypeName(type.getName());
            }
        }
        return pager;
    }

    @Override
    public List<OpinionTypeSelectOptionVo> opinionType() {
        return typeDao.getSelectOptions();
    }

    @Override
    public OpinionInfoVo get(String id) {
        OpinionInfoVo vo = dao.getOpinionInfoVo(id);
        Assert.notNull(vo, "该反馈信息不存在");
        vo.setTypeName(typeDao.getTypeName(vo.getTypeId()));
        vo.setResponseUserName(userDao.getUserNameById(vo.getResponseUserId()));
        return vo;
    }

    @Override
    public int updateResponse(UpdateResponseDto dto) {
        String id = dto.getId(), response = dto.getResponse();
        SystemOpinion systemOpinion = dao.selectById(id);
        Assert.notNull(systemOpinion, "该反馈信息不存在");
        Assert.notEquals(systemOpinion.getAlreadyResponse(), ResponseStatus.RESPONSE.getCode(), "该反馈信息已被回复");
        UserVo currentUser = userHolderUtil.getCurrentUser();
        Date now = new Date();
        systemOpinion.setUpdateInfo(currentUser.getId(), now);
        systemOpinion.setAlreadyResponse(ResponseStatus.RESPONSE.getCode());
        systemOpinion.setResponse(response);
        systemOpinion.setResponseUserId(currentUser.getId());
        systemOpinion.setResponseTime(now);
        return dao.updateById(systemOpinion);
    }
}
